package top.milkbox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import top.milkbox.domain.MenuSimple;
import top.milkbox.domain.Userinfo;
import top.milkbox.service.ClickService;
import top.milkbox.service.CollectionService;
import top.milkbox.service.MenuSimpleService;
import top.milkbox.mapper.MenuSimpleMapper;
import org.springframework.stereotype.Service;
import top.milkbox.service.PraiseService;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author xiaoguo
 * @description 针对表【tb_menu_simple】的数据库操作Service实现
 * @createDate 2022-04-26 20:34:52
 */
@Service
public class MenuSimpleServiceImpl extends ServiceImpl<MenuSimpleMapper, MenuSimple>
        implements MenuSimpleService {

    @Autowired
    private MenuSimpleMapper menuSimpleMapper;

    @Autowired
    private ClickService clickService;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private PraiseService praiseService;

    @Override
    public List<MenuSimple> findByToolsAndStuffLike(List<String> toolsList, List<String> stuffList) {
        QueryWrapper<MenuSimple> wrapper = new QueryWrapper<>();

        // 添加stuff模糊条件
        wrapper.and(menuSimpleQueryWrapper -> {
            for (int i = 0; i < stuffList.size() - 1; i++) {
                menuSimpleQueryWrapper.like("stuff", stuffList.get(i));
                menuSimpleQueryWrapper.or();
            }
            menuSimpleQueryWrapper.like("stuff", stuffList.get(stuffList.size() - 1));

        });

        // 添加tools模糊条件
        wrapper.and(menuSimpleQueryWrapper -> {
            for (int i = 0; i < toolsList.size() - 1; i++) {
                menuSimpleQueryWrapper.like("tools", toolsList.get(i));
                menuSimpleQueryWrapper.or();
            }
            menuSimpleQueryWrapper.like("tools", toolsList.get(toolsList.size() - 1));
        });

        return menuSimpleMapper.selectList(wrapper);
    }

    @Override
    public List<MenuSimple> findAllOrderByClickCount() {
        return menuSimpleMapper.selectOrderClickCount();
    }

    @Override
    public List<MenuSimple> findAllByGender(Integer gender) {
        return menuSimpleMapper.selectClickCountByGender(gender);
    }

    @Override
    public MenuSimple findOneById(Integer id, HttpSession session) {
        Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
        userinfo.setId(51);
        // 判断userinfo是否为空
        if (userinfo != null) {
            // 根据id查询一条菜的信息
            MenuSimple menuSimple = menuSimpleMapper.selectById(id);
            // 根据id查询点赞量
            menuSimple.setClickCount(clickService.getClickByMenuId(id));
            // 根据id查询收藏量
            menuSimple.setCollectionCount(collectionService.selectCountByMenuId(id));
            // 根据id查询点赞量
            menuSimple.setPraiseCount(praiseService.selectCountByMenuId(id));
            // 用户是否收藏
            menuSimple.setIsCollection(collectionService.isCollection(userinfo.getId(), id));
            // 用户是否点赞
            menuSimple.setIsPraise(praiseService.isPraise(userinfo.getId(), id));

            return menuSimple;
        }
        return null;
    }

}




